package org.example.examfx02.controller;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.example.examfx02.entity.Course;
import org.example.examfx02.entity.Teacher;
import org.example.examfx02.HelloApplication;
import org.example.examfx02.tool.SystemData;
import org.example.examfx02.tool.WriteFile;

import java.util.List;

public class TeaViewController {
    public static String id,type;
    Teacher curUser = (Teacher) SystemData.getCurUser();
    SystemData sys = SystemData.getInstance();
    List<Course> list = sys.getCourseByTeacherId(curUser.getWorkId());
    @FXML
    Label welcomeText;
    @FXML
    Button saveBtn;
    @FXML
    Button showBtn;
    @FXML
    Button changeBtn;
    @FXML
    TableView table;
    @FXML
    TableColumn<Course,String> courseId;
    @FXML
    TableColumn<Course,String> courseName;
    @FXML
    TableColumn<Course,String> courseNum;
    @FXML
    TableColumn<Course,String> courseType;
    @FXML
    TableColumn<Course,Integer> courseCredit;
    @FXML
    TableColumn<Course,Integer> courseMaxNum;

    public void initTable(){
        courseId.setCellValueFactory(new PropertyValueFactory<>("id"));
        courseName.setCellValueFactory(new PropertyValueFactory<>("name"));
        courseNum.setCellValueFactory(new PropertyValueFactory<>("chooseNum"));
        courseType.setCellFactory(column->new TypeCell());
        courseCredit.setCellFactory(column -> new CreditCell());
        courseMaxNum.setCellFactory(column -> new MaxNumCell());
        // 为公共列设置居中显示
        courseId.setCellFactory(MainViewController.createCenteredCellFactory());
        courseName.setCellFactory(MainViewController.createCenteredCellFactory());
        courseNum.setCellFactory(MainViewController.createCenteredCellFactory());
        table.setItems(FXCollections.observableArrayList(list));
    }
    @FXML
    public void initialize(){
        welcomeText.setText("欢迎你,"+SystemData.getCurUser().getUsername()+"教师");
        initTable();
    }
    public void changePassword(){
        HelloApplication.addView("changePassword-view.fxml",400,290,"修改密码");
    }
    public void save(){
        WriteFile.writeTeaFile(sys.getUserGroup().getTeachers());
    }
    public void show(){
        Course course = (Course) table.getSelectionModel().getSelectedItem();
        if(course!=null){
            id = course.getId();
            if(course.getType()==0){
                type = "必修";
            }else{
                type = "选修";
            }
            HelloApplication.addView("show-view.fxml",328,440,"选课学生");
        }else{
            HelloApplication.showAlert("删除","请先选择课程！！！");
        }
    }
}
